<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initCache()
    {
        $this->bootstrap('Autoload');
        $this->bootstrap('view');
        $resources  = $this->getOption('resources');

        // Do not init cache if the cache is disabled or there is default cache already registered
        if($resources['Cache']['mode'] == "off" || Zend_Registry::isRegistered('cache'))
        {

            return;
        }else{

            try{
                $cache = Zend_Cache::factory('core',$resources['Cache']['backend']['name'],   $resources['Cache']['frontend'],    $resources['Cache']['backend']	);

                // Checking is primary cache working fine
                $cache->save('ping','pong');
                $pong = $cache->load('pong');
                if($pong != 'ping')
                {
                    throw new Exception("Primary cache failed");
                }

            }Catch(Exception $e)
            {
                error_log("Primary cache is not accessible! ".$e->getMessage());
                unset($cache);
                $cache = Zend_Cache::factory('core',$resources['Cache']['backup']['backend']['name'],   $resources['Cache']['frontend'],    $resources['Cache']['backup']['backend']	);

            }

            if(isset($_GET['cache']) )
            {
                $cache->clean(	 Zend_Cache::CLEANING_MODE_MATCHING_TAG,	array($_GET['cache']));
            }

            Zend_Registry::set('cache',$cache);
        }
    }

    protected function _initAutoload()
    {

        $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => '',
            'basePath'  => dirname(__FILE__),
        ));
        $autoloader->addResourceType('HappyCity','..'.DIRECTORY_SEPARATOR.'library'.DIRECTORY_SEPARATOR.'HappyCity','HappyCity_');
        $autoloader->addResourceType('Plugins','plugins','Plugins_');
        $autoloader->addResourceType('Default',dirname(__FILE__),'Default_');

        return $autoloader;
    }


    protected function _initView()
    {
        $view = new Zend_View();

        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
        $viewRenderer->setView($view);
        return $view;

    }


    protected function _initPresentation()
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        
        //@todo MW - Add configurable parameteres here
        $view->doctype('XHTML1_STRICT');
        $view->headMeta()->setHttpEquiv('Content-Type','text/html; charset=utf-8');
        $view->headTitle("Happy City");
    }
    protected function _initActionHelpers()
    {
        Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH."/controllers/helpers");
    }
    protected function _initRoutes()
    {
        $config = new Zend_Config_Ini (		APPLICATION_PATH . '/configs/routes.ini', 'production'	);
        $frontController = Zend_Controller_Front::getInstance();
        $router = $frontController->getRouter();
        $router->addConfig( $config, 'routes' );


    }

    protected function _initRequest(array $options = array())
    {


        // Ensure front controller instance is present, and fetch it
        $this->bootstrap('FrontController');
        $front = $this->getResource('FrontController');

        // Initialize the request object
        $request = new Zend_Controller_Request_Http();

        $request->setBaseUrl($this->getOption('baseurl'));
        $view = $this->getResource('view');
        $view->baseUrl =$this->getOption('baseurl');
        // Add it to the front controller
        $front->setRequest($request);

        // Bootstrap will store this value in the 'request' key of its container
        return $request;
    }


}

